<?php namespace Core\Myapp\View\Admin;

class View extends \Core\Myapp\Zobero
{
    protected $_form;
    protected $_contentData = null;
    protected $_contentTemplate = null;
    protected $_layout = 'two-columns-left';
    protected $_regions = array('header', 'footer', 'left-col', 'right-col', 'top-content', 'bottom-content');
    protected $_parts = array(
        'head'=>'page/parts/head.phtml',
        'header'=>'page/parts/header.phtml',
        'toolbar'=>'page/parts/toolbar.phtml',
        'navigation'=>'page/parts/navigation.phtml'
    );
    protected $_buttons = array();
    protected $_js = array();
    protected $_css = array();
    // Meta Data
    protected $_title;
    protected $_contentType = 'text/html; charset=UTF-8';

    public function getParts()
    {
        return $this->_parts;
    }
    public function setLayout($name)
    {
        $this->_layout = $name;
        return $this;
    }
    public function setContent($data, $template)
    {
        $this->_contentData = $data;
        $this->_contentTemplate = $template;
        return $this;
    }
    public function getContentData()
    {
        return $this->_contentData;
    }
    protected function loadContent()
    {
        $html = $this->fetchTemplate($this->_contentTemplate);
        return $html;
    }
    public function addRegion($name)
    {
        $this->_regions[] = $name;
        return $this;
    }
    public function renderRegion($name)
    {

    }
    public function render()
    {
        $layoutPath = 'page' . DS . $this->_layout . '.phtml';
        $html = $this->fetchTemplate($layoutPath);
        $this->getResponse()->setBody($html)->sendResponse();
    }
    protected function getResponse()
    {
        return $this->getInstance('\Core\Zend\Controller\Response\Http');
    }
    public function loadPart($name)
    {
        $parts = $this->getParts();
        $path = $parts[$name];
        return $this->fetchTemplate($path);
    }
    protected function fetchTemplate($path)
    {
        $filePath = BACKEND_DESIGN_PATH . DS . 'default' . DS . 'template' . DS . $path;
        ob_start();
        try {
            include $filePath;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
        $html = ob_get_clean();
        return $html;
    }
    public function setTitle($title)
    {
        $this->_title = $title;
    }
    public function setContentType($contentType)
    {
        $this->_contentType = htmlspecialchars($contentType);
    }
    public function getTitle()
    {
        return $this->_title;
    }
    public function getContentType()
    {
        return $this->_contentType;
    }
    public function addJs($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        $this->_js[] = $url->getJsUrl($path);
        return $this;
    }
    public function addCss($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        $this->_css[] = $url->getCssUrl($path);
        return $this;
    }
    public function loadJs()
    {
        $js = '';
        foreach($this->_js as $key=>$path) {
            $js .= '<script src="' . $path . '"></script>\n';
        }
        return $js;
    }
    public function loadCss()
    {
        $css = '';
        foreach($this->_css as $key=>$path) {
            $css .= '<link rel="stylesheet" href="'.$path.'" media="all" />\n';
        }
        return $css;
    }
    public function getCssUrl($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        return $url->getCssUrl($path);
    }
    public function getJsUrl($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        return $url->getJsUrl($path);
    }
    public function addButton($title, $class, $onclick)
    {
        $this->_buttons[] = array(
            'name'   =>$title,
            'class'  =>$class,
            'onclick'=>$onclick
        );
        return $this;
    }
    public function getButtons()
    {
        return $this->_buttons;
    }
    public function getForm()
    {
        if(!empty($this->_form))
            return $this->_form;
        else
        {
            $this->_form = $this->getInstance('Core\Myapp\Helper\Admin\Form\Form');
            return $this->_form;
        }
    }
}